
#include <pwd.h>
#include <stdio.h>
#include <ctype.h>
#include <unistd.h> 
#include <string.h>
#include <sys/types.h> 
#include <sys/stat.h>
#include "Global.h"
#include "ParseCommand.h"
#include "SearchServer.h"
#include "DisplayResult.h"
#include "ContentSearch.h"

extern CSearchServer*	searchserver;
extern CContentSearch*	content;
extern CParseCommand*	parse;


/******************************************
* ������CDisplayResult()                *
* ��  ��: ���캯������׺����ID��Map *
*         ����û�������ļ����ͶԸ�����  *
*         �����к�׺��־Ϊ������ʾ        *
*******************************************/
CDisplayResult::CDisplayResult()
{
	int		i;
	FILE*	fp;
	char	currentline[300];
	char	extendname[100];
	char	description[100];
	int		filetype;
	int		extendid;


	m_iSearchSizeStart	=parse->m_iSearchSizeStart;
	m_iSearchSizeEnd	=parse->m_iSearchSizeEnd;

	m_iSearchTimeStart	=parse->m_iSearchTimeStart;
	m_iSearchTimeEnd	=parse->m_iSearchTimeEnd;

	m_iSearchType		=parse->m_iSearchType;
	strcpy(m_sSearchDirectory, parse->m_sSearchDirectory);

	m_bDisplayType=new char[searchserver->m_iExtendNumber+1];
	memset(m_bDisplayType,1, searchserver->m_iExtendNumber+1);

	if ( strcmp(parse->m_sSearchContent,"") )
	{
		parse->m_iSearchType=1;
	}

	if (parse->m_iSearchType)
	{
		fp=fopen("/data/data/com.example.anything/FileType.txt","rb");
		if (fp==NULL)
		{
		printf("ϵͳ�ļ���ʧ,���뿪������ϵ!�쳣��Ϊ�ļ� %s �ĵ� %d ��\n", __FILE__,__LINE__);
			exit(-4);
		}

		memset(m_bDisplayType,0, searchserver->m_iExtendNumber+1);
		while (!feof(fp))
		{
			fgets(currentline, 3000, fp);
			if (currentline[0]=='#' || currentline[0]==' ') continue;
			i=sscanf(currentline, "%s\t%d\t%d\t%s\n", extendname, &extendid, &filetype, description);
			if (i<=0)
			{
				break;
			}
			if (filetype==parse->m_iSearchType)
			{
				m_bDisplayType[extendid]=1;
			}
		}
		fclose(fp);
	}


	fp=fopen("/data/data/com.example.anything/AllExtend.txt", "rb");
	if (fp==NULL)
	{
		printf("ϵͳ�ļ���ʧ,���뿪������ϵ!�쳣��Ϊ�ļ� %s �ĵ� %d ��\n", __FILE__,__LINE__);
		exit(-3);
	}
	while (!feof(fp))
	{
		i=fscanf(fp, "%s\t%d\n", extendname, &extendid);
		if (i<=0)
		{
			break;
		}
		m_ExtendMap[string(extendname)]=extendid;
	}
	fclose(fp);

}


/*******************************************
* ������ParseAccessMode()                *
* ��  ��: ������ǰ��¼�ķ���ģʽ������û� *
*         ���Է��ʸ��ļ����򷵻�0������-1  *
********************************************/
int	CDisplayResult::ParseAccessMode()
{
	int				i;
	unsigned short	mode;
	unsigned short	bMode;
	char*			pos;
	struct	passwd* pwd; 
	unsigned short  access;
	
	//�ж�����������û��Ƿ��з��ʵ�ǰ��¼�ļ���Ȩ��
	sscanf(m_sCurrentModeUserID+1, "%u %d", &mode, &m_iCurrentOwnerID);
	access=mode%8;

	if (m_iCurrentUserID!=m_iCurrentOwnerID && m_iCurrentUserID!=0 && access<4)
	{
		return 1;
	}

	//�趨��ǰ�û������ʾ
	memset(m_sCurrentOwner,0, 300);
	pwd=getpwuid(m_iCurrentOwnerID);
	if (pwd!=NULL)
	{
		strcpy(m_sCurrentOwner, pwd->pw_name);
		for(i=0; i<12; i++)
		{
			if (m_sCurrentOwner[i]==0)
			{
				m_sCurrentOwner[i]=' ';
			}
		}
		if (m_sCurrentOwner[12]!=0)
		{
			m_sCurrentOwner[10]='.';
			m_sCurrentOwner[11]='.';
			m_sCurrentOwner[12]=0;
		}
	}
	else
	{
		strcpy(m_sCurrentOwner,"Unknown     ");
	}

	//���õ�ǰ��¼�ķ���ģʽ����ʾ
	mode=mode%512;
	memset(m_sCurrentMode,0, 11);
	m_sCurrentMode[0]=m_sCurrentModeUserID[0];

	pos=m_sCurrentMode+1;
	for (i=0; i<9; i++)
	{
		access=1<<(8-i);
		bMode=mode & access;
		if (bMode)
		{
			switch(i%3) 
			{
			case 0 :*pos='r';
					break;
			case 1 :*pos='w';
					break;
			case 2 :*pos='x';
					break;
			}
		}else
		{
			*pos='-';
		}
		pos++;
	}

	return 0;	
}


/*******************************************
* ������DisplayTime()                    *
* ��  ��: ��ʽ����ʾ�ļ����޸�ʱ��         *
*         m_iCurrentTime���浱ǰ��¼���޸� *
*         ʱ�䣬����ת��Ϊ������ʱ������ʾ *
********************************************/
int	CDisplayResult::DisplayTime()
{
	time_t	ct;
	struct	tm *p;

	//��ȡ�ļ��޸�ʱ���������ʱ����tm�ṹ
	ct=(time_t)m_iCurrentTime;
	p=gmtime(&ct);

	//��ʾ��
	printf("%d/",(1900+p->tm_year));

	//��ʾ�·�
	if (p->tm_mon<9)
	{
		printf("0");
	}
	printf("%d/",1+p->tm_mon);

	//��ʾ����
	if (p->tm_mday<10)
	{
		printf("0");
	}
	printf("%d ",p->tm_mday);

	//��ʾʱ
	if (p->tm_hour<10)
	{
		printf("0");
	}
	printf("%d:",p->tm_hour);

	//��ʾ��
	if (p->tm_min<10)
	{
		printf("0");
	}
	printf("%d:",p->tm_min);

	//��ʾ��
	if (p->tm_sec<10)
	{
		printf("0");
	}
	printf("%d ",p->tm_sec);

	return 0;
}


/******************************************
* ������DisplayCurrentLine()            *
* ��  ��: ����û�����Ĳ�����ʾ��ǰ��¼  *
*******************************************/
int	CDisplayResult::DisplayCurrentLine()
{
	char*	pos;
	char	extend[300];
	char	ch;
	int     i;
	int     j;
	int		start;
	int		end;
	int		length;
	int		result;


	m_iCurrentTime=0;
	m_iCurrentSize=0;
	memset(extend,				0, 300);
	memset(m_sCurrentFile,		0, 3000);
	memset(m_sCurrentDirectory,	0, 3000);
	memset(m_sCurrentModeUserID,0, 300);

	//��ȡ��ǰ��¼�ķ���ģʽ
	end	 =0;
	length=strlen(m_sCurrentLine);
	while (m_sCurrentLine[end]!=9 && end<length)
	{
		end++;
	}
	memcpy(m_sCurrentModeUserID, m_sCurrentLine, end);

	//������ǰ��¼�ķ���ģʽ
	result=ParseAccessMode();
	if (result!=0)
	{
		return 1;
	}

	//��ȡ��ǰ��¼�ļ���
	end++;
	start=end;
	while (m_sCurrentLine[end]!=9 && end<length)
	{
		end++;
	}
	memcpy(m_sCurrentFile, m_sCurrentLine+start, end-start);

	//��ȡ��ǰ��¼��Ŀ¼
	end++;
	start=end;
	while (m_sCurrentLine[end]!=9 && end<length)
	{
		end++;
	}
	memcpy(m_sCurrentDirectory, m_sCurrentLine+start, end-start);

	//��ȡ��ǰ��¼���ļ���С
	m_iCurrentSize=0;
	pos=m_sCurrentLine+end+1;
	while (isdigit(*pos))
	{
		m_iCurrentSize=m_iCurrentSize*10+*pos-'0';
		pos++;
	}
	pos++;

	//��ȡ��ǰ��¼������޸�ʱ��
	m_iCurrentTime=0;
	while (isdigit(*pos))
	{
		m_iCurrentTime=m_iCurrentTime*10+*pos-'0';
		pos++;
	}

	//��ȡ��ǰ��¼�ļ��ĺ�׺��
	m_iExtendID=0;
	if (m_sCurrentLine[0]=='_')
	{
		length=strlen(m_sCurrentFile);
		for (i=length; i>=0; i--)
		{
			if (m_sCurrentFile[i]=='.')
			{
				break;
			}
		}

		//��ǰ�ļ��к�׺��
		if(i!=-1)
		{
			strcpy(extend,m_sCurrentFile+i+1);
			length=strlen(extend);
			for (j=0; j<length; j++)
			{
				if (extend[j]<='Z' && extend[j]>='A')
				{
					extend[j]=extend[j]+32;
				}
			}
			m_It=m_ExtendMap.find(string(extend));
			if (m_It!=m_ExtendMap.end())
			{
				m_iExtendID=m_It->second;
			}
			else
			{
				m_iExtendID=0;
				return 0;
			}
		}
	}
	
	//����ǰ��¼��Ŀ¼��ΪСд
	memset(m_sCurrentDirectoryLower,0, 3000);
	strcpy(m_sCurrentDirectoryLower, m_sCurrentDirectory);
	pos=m_sCurrentDirectoryLower;
	while(*pos)
	{
		if (*pos>'A' && *pos<'Z')
		{
			*pos=*pos+32;
		}
		pos++;
	}

	length=strlen(m_sSearchDirectory);

	//������ǰ��¼�ļ��Ƿ����û������ѯ��	
	m_iContentResult=1;
	if ( strcmp(parse->m_sSearchContent,"") && m_bDisplayType[m_iExtendID] )
	{
		sprintf(m_sCurrentFileId, "%s/%s",m_sCurrentDirectory, m_sCurrentFile);
		m_iContentResult=content->ContentSearch(m_sCurrentFileId);
	}

	//���ǰ��¼�����û���������в�������ʾ
	if (  m_iCurrentSize>=m_iSearchSizeStart &&
		  m_iCurrentSize<=m_iSearchSizeEnd   &&
		  m_iCurrentTime>=m_iSearchTimeStart &&
		  m_iCurrentTime<=m_iSearchTimeEnd   &&
		  m_bDisplayType[m_iExtendID]        &&
		  m_iContentResult>0				 &&
		  memcmp(m_sCurrentDirectoryLower, m_sSearchDirectory,length)==0    )
	{
		printf("%s ", m_sCurrentMode);				//��ʾ��ǰ��¼�ķ���ģʽ
		printf("%s\t", m_sCurrentOwner);			//��ʾ��ǰ��¼��������
		DisplayTime();								//��ʾ��ǰ��¼���޸�ʱ��
		printf("%12u\t",	m_iCurrentSize);		//��ʾ��ǰ��¼���ļ���С
		printf("%s/",		m_sCurrentDirectory);	//��ʾ��ǰ��¼��Ŀ¼	
		printf("%s\n",		m_sCurrentFile);		//��ʾ��ǰ��¼���ļ���

		m_iDisplayResultNumber++;
		if (m_iDisplayResultNumber%35==34)
		{
			printf("���س�����ʾ��һҳ, 'q' �� 'Q'�˳�.....\n");
			ch=getchar();
			if (ch=='q' || ch=='Q')
			{
				return -2;
			}
		}
	}


	return 0;
}


/**************************************
* ������DisplayAll()                *
* ��  ��: ��ʾ�û���ѯ�����н���ļ�  *
***************************************/
int CDisplayResult::DisplayAll()
{
	uint32	i;
	FILE*	fp;
	char*	pos;
	int		result;

	//����û����������ݲ�ѯ������Ըò�ѯ������Ԥ����
	if (strcpy(content->m_sSearchQuery,""))
	{
		result=content->PreProcess();
		if (result)
		{
			printf("���ݲ�ѯ�ַ�Ԥ��������޷����ϵ��쳣�����뿪������ϵ���쳣��Ϊ�ļ� %s �ĵ� %d �С�\n",__FILE__,__LINE__);
			exit(-2);
		}
	}

	//��ȡ��ǰ��������ߵ�id
	m_iCurrentUserID=getuid();

	//�򿪱���Ӳ���ļ�ϵͳ��Ϣ�ĵ��ļ�
	fp=fopen("/usr/local/gsearch/AllFile.dat","rb");
	if (fp==NULL)
	{
		printf("��ϵͳ�ļ�ʧ�ܣ����뿪������ϵ���쳣��Ϊ�ļ� %s �ĵ� %d ��\n", __FILE__, __LINE__);
		exit(-2);
	}
	m_iDisplayResultNumber=0;


	//���û���������Ŀ¼��ΪСд
	pos=m_sSearchDirectory;
	while(*pos)
	{
		if (*pos>'A' && *pos<'Z')
		{
			*pos=*pos+32;
		}
		pos++;
	}

	printf("\n");

	//���������������ص�ÿһ����¼�ж��Ƿ����û���ѯ
	for( i=0; i<searchserver->m_iResultNumber; i++)
	{
		fseek(fp, searchserver->m_iQueryResult[i], SEEK_SET);
		fgets(m_sCurrentLine, 3000, fp);
		result=DisplayCurrentLine();	
		if (result<0)
		{
			break;
		}
	}

	printf("\n���ҵ��ļ� %d ��\n", m_iDisplayResultNumber);
	return 0;

}


